package com.example.car.controller;

import com.example.car.entity.User;
import com.example.car.entity.Driver;
import com.example.car.model.IdNameModel;
import com.example.car.params.UserParams;
import com.example.car.service.UserService;
import com.example.car.utils.JwtUtil;
import com.example.car.vo.CommonResult;

import com.example.car.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "用户接口")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("list")
    @ApiOperation("用户信息")
    public CommonResult getCustomerList(@RequestBody UserParams memberParams) {
        List<User> list = userService.getCustomerList(memberParams);
        return CommonResult.success().data("data", list);
    }

    @GetMapping("selectOne")
    @ApiOperation("用户信息")
    public CommonResult selectOneById(@PathVariable("userId") String userId) {
        User user = userService.getUserByUserId(userId);
        return CommonResult.success().data("data", user);
    }

    @PostMapping("update")
    @ApiOperation("修改用户信息")
    public CommonResult updateUser(@RequestBody UserVo userVo) {
        userService.updateUser(userVo);
        return CommonResult.success();
    }

    @DeleteMapping("delete")
    @ApiOperation("删除用户")
    public CommonResult deleteUser(String userId, HttpServletRequest request) {
        String role = JwtUtil.getUserRoleByToken(request);
        if (!"管理员".equals(role)) {
            return CommonResult.error("权限不足");
        }
        if (StringUtils.isEmpty(userId)) {
            return CommonResult.error("用户id为空");
        }
        String result = userService.deleteUserById(userId);
        if (result != null) {
            return CommonResult.error("系统异常");
        }
        return CommonResult.success();
    }

}
